package com.lce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lce.beans.Dept;
import com.lce.beans.User;
import com.lce.dao.CeoDao;
import com.lce.service.CeoService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class CeoServiceImpl implements CeoService {
    private SqlSessionTemplate sqlSessionTemplate;

    @Resource
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }



    @Override
    public HashMap<String, Object> getalluser(Integer pageNo, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNo, pageSize);
        CeoDao ceoDao = sqlSessionTemplate.getMapper(CeoDao.class);
        List<User> users = ceoDao.getAllUser();
        //用PageInfo对结果进行包装

        PageInfo<User> pageInfo = new PageInfo<>(users);
        HashMap<String, Object> map = new HashMap<>();
        //返回页面数据
        map.put("data",pageInfo.getList());
        //返回当前是第几页
        map.put("nowPage",pageNo);
        //返回总页数
        map.put("total",((int)pageInfo.getTotal()+pageSize-1)/pageSize);



        return map;
    }

    @Override
    public long counts() {
        CeoDao ceoDao = sqlSessionTemplate.getMapper(CeoDao.class);
        long counts = ceoDao.getCounts();
        return counts;
    }


    @Override
    public List<Dept> ceoGetAllDepts() {
        CeoDao ceoDao = sqlSessionTemplate.getMapper(CeoDao.class);
        List<Dept> depts = ceoDao.getAllDepts();
        for (Dept dept : depts) {
            System.out.println("部门为"+dept);
        }
        return depts;
    }

    @Override
    public void addUser(User user) {
        CeoDao ceoDao = sqlSessionTemplate.getMapper(CeoDao.class);
        ceoDao.addUser(user);

    }

    @Override
    public void deleteUser(int uid) {
        CeoDao ceoDao = sqlSessionTemplate.getMapper(CeoDao.class);
        ceoDao.deleteUser(uid);
    }

    @Override
    public User getUserById(int id) {
        CeoDao ceoDao = sqlSessionTemplate.getMapper(CeoDao.class);
        User user =ceoDao.getUserById(id);
        return user;
    }

    @Override
    public void updateUser(User user) {
        CeoDao ceoDao = sqlSessionTemplate.getMapper(CeoDao.class);
        ceoDao.updateUser(user);

    }
}
